package com.live.task;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.util.Log;

import com.live.common.Constant;
import com.live.tools.LiveTools;

public class LiveWelcome extends AsyncTask<String, Integer, String> {

	private Context context = null;

	private String deviceID = null;

	public LiveWelcome(Context context, String deviceID) {
		this.context = context;
		this.deviceID = deviceID;
	}

	// 此方法在主线程执行,当任务执行之前开始调用此方法，可以在这里显示进度对话框
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		Log.i("Comment", "onPreExecute()  " + Thread.currentThread().getId());
	}

	// 此方法在后台线程执行，完成任务的主要工作，通常需要较长的时间。
	// 在执行过程中可以调用publicProgress(Progress...)来更新任务的进度。
	@Override
	protected String doInBackground(String... params) {
		Log.i("Comment", "doInBackground... " + Thread.currentThread().getId());

		// 此URL记录访问者
		SharedPreferences store = context.getSharedPreferences(Constant.PREFS_NAME, 0);
		String url = Constant.SITE_URL + "live_welcome.php?locale=" + LiveTools.getLocaleLanguage() + "&";
		url += "userNo=" + store.getString("id", null) + "&deviceId=" + deviceID;

		welcome(url);

		publishProgress();
		return null;
	}

	// 此方法在主线程执行，用于显示任务执行的进度
	@Override
	protected void onProgressUpdate(Integer... values) {
		super.onProgressUpdate(values);
		Log.i("PageTask", "onProgressUpdate()  " + Thread.currentThread().getId());
	}

	// 此方法在主线程执行，任务执行的结果作为此方法的参数返回。
	@Override
	protected void onPostExecute(String result) {
		// tv.setText(result);
		Log.i("PageTask", "onPostExecute()  " + Thread.currentThread().getId());
	}

	private List<Map<String, Object>> welcome(String url) {
		List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
		HttpClient client = new DefaultHttpClient();
		// 提拱默认的HttpClient实现
		HttpPost request;
		try {
			request = new HttpPost(new URI(url));
			client.execute(request);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return data;
	}

}